#include "shell_port.h"
#include "stdio.h"
#include "string.h"
#include "tusb.h"

Shell shell;
char shell_buffer[512];

//log日志的代码，实现日志控制和多色日志
#if SHELL_USING_CMD_LOG

#include "log.h"
#define true 1

void USB_LogWrite(char * buffer,short len);
Log usbLog = {
   .write = USB_LogWrite,
   .active = true,
   .level = LOG_DEBUG
};
void USB_LogWrite(char * buffer,short len)
{
   if(usbLog.shell)
   {
       shellWriteEndLine(usbLog.shell,buffer,len);
   }
}
#endif


//底层代码(USB_CDC接口相关的实现代码)

// shell接口文件
signed short USER_ShellWrite(char * data, unsigned short len)              
{
    uint16_t time_out = 60000;
    uint8_t res = 0;
	tud_cdc_write(data, len);
    tud_cdc_write_flush();
    return (short)len;
}


void User_ShellTask(void)
{ 
	while(tud_cdc_available()){
        int32_t rx_char = tud_cdc_read_char();
        if(rx_char != -1){
            shellHandler(&shell,(char)rx_char);
        }
    }	
} 


void User_Shell_Init(void)
{
    // 初始化使能USB的部分
    shell.write = USER_ShellWrite; //挂上写函数
    shellInit(&shell,shell_buffer,sizeof(shell_buffer)); //设置缓冲区
#if SHELL_USING_CMD_LOG
     logRegister(&usbLog,&shell);
#endif
}








